﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace CYTS.Aoyou.LuceneSearch
{
    public class DataAccess
    {
        static string connStr = ConfigurationManager.ConnectionStrings["Search"].ConnectionString;

        /// <summary>
        /// 通过存储过程得到DataSet
        /// </summary>
        /// <param name="SP">存储过程名</param>
        /// <param name="Par">参数数组</param>
        /// <returns></returns>
        public static DataSet GetDataSetBySP(string SP, SqlParameter[] Par)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(SP, new SqlConnection(connStr));
            try
            {
                cmd.CommandType = CommandType.StoredProcedure;
                for (int i = 0; i <= Par.GetUpperBound(0); i++)
                {
                    cmd.Parameters.Add(Par[i]);
                }
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                cmd.Connection.Close();
                cmd.Connection.Dispose();
                cmd.Dispose();
            }
            return ds;
        }

        /// <summary>
        /// 通过存储过程得到DataSet
        /// </summary>
        /// <param name="SP">存储过程名</param>
        /// <returns></returns>
        public static DataSet GetDataSetBySP(string SP)
        {
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(SP, new SqlConnection(connStr));
            try
            {
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                cmd.Connection.Close();
                cmd.Connection.Dispose();
                cmd.Dispose();
            }
            return ds;
        }

        /// <summary>
        /// 执行没有返回值的存储过程
        /// </summary>
        /// <param name="SP">存储过程名</param>
        /// <param name="Par">参数数组</param>
        /// <returns></returns>
        public static void ExecuteSP(string SP, SqlParameter[] Par)
        {
            SqlCommand cmd = new SqlCommand(SP, new SqlConnection(connStr));
            try
            {
                cmd.Connection.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                for (int i = 0; i <= Par.GetUpperBound(0); i++)
                {
                    cmd.Parameters.Add(Par[i]);
                }
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                cmd.Connection.Close();
                cmd.Connection.Dispose();
                cmd.Dispose();
            }
        }
    }
}
